#!coding:utf-8
import locale # for proper curses unicode support
locale.setlocale(locale.LC_ALL,"")

from wikiqueries import toU
charlen = lambda x: len(toU(x))

from filedict import FileDict
persons = FileDict("dictionaries/personlist")
iwikis = FileDict("dictionaries/iwikis")

import curses
screen = curses.initscr()
curses.noecho() # pressed keys don't displayed on screen
curses.curs_set(0) # cursor not visible
curses.start_color()
curses.init_pair(1,curses.COLOR_WHITE,curses.COLOR_BLACK)
curses.init_pair(2,curses.COLOR_RED,curses.COLOR_BLACK)
curses.init_pair(3,curses.COLOR_GREEN,curses.COLOR_BLACK)

def safeprint(self,y,x,msg, col = 1):
	if x< 0 or y < 0 : return
	maxy,maxx = self.getmaxyx()	
	if y>=maxy: return
	if x>=maxx: return
	l = charlen(msg)
	d= maxx - (x+l)-1
	if d<0:
		msg = (toU(msg)[:d]).encode('utf-8') # oh, how long i can't find bug here
		self.addstr(y,x,msg,curses.color_pair(col))
	else:
		self.addstr(y,x,msg,curses.color_pair(col))

variant_keys = "jfkdlshg;a"
def menu(variants,y,x):
	for i,v in enumerate(variants):
		safeprint(screen,y+i*2,x,variant_keys[i]+":"+v,3)
	screen.refresh()
	x = screen.getch()	
	try:
		var = variant_keys.index(chr(x))
	except ValueError:
		var = -1
	return var

for eperson in persons.keys():
	try:
		uperson = iwikis[]
	except KeyError:
		continue
	uk = uperson.split()
	en = eperson.split()		
	for i,v in enumerate(en):
		safeprint(screen,4+i*2,10,v)
	v = menu(uk,4,100)
	if v<0:
		break

curses.echo()
curses.endwin()
